/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.server;

import com.ps.server.classes.configurations;
import com.ps.server.classes.taxes;
import java.util.ArrayList;
import java.util.Hashtable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Ricardo
 */
public class TaxesLibraryXMLReader extends DefaultHandler {

    Hashtable nodos = new Hashtable();
    private taxes nodoActualConfigurations = new taxes();
    String resource;
    private String contenido;
    private langTemp tem;
    //private clsProduct nodoActual = new clsProduct();
    private ArrayList<langTemp> descriptionLanguage;

    public TaxesLibraryXMLReader(Hashtable nodos) {
        this.nodos = nodos;
    }

    public TaxesLibraryXMLReader() {
    }
    /*
     * Esta funcion el llamada cuando se produce el evento de ver una nueva
     * etiqueta
     */

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) {

        if ("taxes".equals(qName)) {
            this.resource = qName;
            nodoActualConfigurations = new taxes();
        }

        if ("tax".equals(qName)) {
            nodoActualConfigurations = new taxes();
        }
        if("name".equals(qName)){
        this.tem = new langTemp();
        this.descriptionLanguage = new ArrayList();
        }
        if ("language".equals(qName)) {
            this.tem.setId(attributes.getValue("id"));
            //this.short_description
            // this.idlang=attributes.getValue("id");
            //this.meta_keywords = new ArrayList();
        }
    }
    /*
     * Esta funcion es llamada cuando ve el contenido de una etiqueta
     */

    @Override
    public void characters(char buf[], int offset, int len)
            throws SAXException {
        contenido = new String(buf, offset, len);

    }
    /*
     * y esta al llegar al final
     */

    @Override
    public void endElement(String uri, String localName, String qName) {
        if (this.resource.equals("taxes")) {
            resourceCategories(qName);
        }
    }

    void resourceCategories(String qName) {
        if ("id".equals(qName)) {
            // aqui se ya se sabe  el orden de  inicializacion de las variables.
            this.nodoActualConfigurations.setId(contenido);
        }

        if ("rate".equals(qName)) {
            nodoActualConfigurations.setRate(Double.valueOf(contenido));
        }
        if ("account_number".equals(qName)) {
            nodoActualConfigurations.setAccount_number(contenido);
        }
        if ("active".equals(qName)) {
            nodoActualConfigurations.setActive(contenido);
        }

        if ("deleted".equals(qName)) {
            nodoActualConfigurations.setDeleted(contenido);
        }

        if ("name".equals(qName)) {
            String[][] shortString = new String[this.descriptionLanguage.size()][2];
            for (int x = 0; this.descriptionLanguage.size() > x; x++) {
                shortString[x][0] = this.descriptionLanguage.get(x).getId();
                shortString[x][1] = this.descriptionLanguage.get(x).getValue();
                // shortString[x][0]=this.idlang;
            }
            this.descriptionLanguage.clear();
            nodoActualConfigurations.setName(shortString);
//            nodoActualCategories.setName(contenido);
        }
        //private CategoryLibraryXMLReader.langTemp tem;
        if ("language".equals(qName)) {
            langTemp t = new langTemp();
            t.setId(this.tem.getId());
            t.setValue(contenido);
            this.descriptionLanguage.add(t);

            //nodoActualManufacturers.setId(contenido);
        }


        if ("tax".equals(qName)) {

            if (!this.nodos.containsKey(this.nodoActualConfigurations.getId())) {
                this.nodos.put(this.nodoActualConfigurations.getId(), this.nodoActualConfigurations);
            }
        }



    }

    class langTemp {

        private String id;
        private String value;

        public void langTemp() {
        }

        public void langTemp(String id, String value) {
            this.setId(id);
            this.setValue(value);
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getValue() {
            if (value == null) {
                value = "";
            }
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}